﻿#include "readlog.h"
#include "ui_readlog.h"

Readlog::Readlog( QWidget* parent ) : QDialog( parent ), ui( new Ui::Readlog ) {
    ui->setupUi( this );

    textedit = new QTextEdit( this );
    textedit->setGeometry( 500, 50, 460, 530 );
    textedit->setFont( QFont( "song", 10 ) );
    textedit->setLineWrapMode( QTextEdit::NoWrap );

    table = new QTableWidget( 1, 3, this );
    table->setGeometry( 10, 50, 460, 530 );
    table->setFont( QFont( "song", 10 ) );
    table->setSelectionBehavior( QAbstractItemView::SelectRows );  //选择整行

    labels << tr( "文件名" ) << tr( "大小" ) << tr( "修改时间" );
    table->setHorizontalHeaderLabels( labels );
    table->horizontalHeader()->setStretchLastSection( true );  //行头自适应表格
    table->horizontalHeader()->setFont( QFont( "song", 12 ) );
    table->horizontalHeader()->setSectionResizeMode( 0, QHeaderView::Stretch );

    table->verticalHeader()->hide();
    table->setShowGrid( true );                                            //网格
    table->setEditTriggers( QAbstractItemView::NoEditTriggers );           //不可编辑
    table->setStyleSheet( "selection-background-color:rgb(150,190,60)" );  //选中颜色
}

Readlog::~Readlog() {
    delete ui;
}

void Readlog::findfiles( QString the_path, QString type ) {
    table->clear();
    table->setHorizontalHeaderLabels( labels );

    QDir*       dir = new QDir( the_path );
    QStringList filter;  //筛选器
    if ( type != "Info" ) { filter.push_back( tr( "*.txt" ) ); }
    else {
        filter.push_back( tr( "*.csv" ) );
    }
    fileInfo = new QList< QFileInfo >( dir->entryInfoList( filter ) );
    int size = fileInfo->count();
    // qDebug() << "size:" << fileInfo->count();
    if ( size <= 0 ) { return; }
    table->setRowCount( size );
    for ( int row = 0; row < size; row++ ) {
        //文件名
        table->setItem( row, 0, new QTableWidgetItem( fileInfo->at( row ).fileName() ) );
        //大小
        int size_kb = static_cast< int >( fileInfo->at( row ).size() / 1024 );
        if ( size_kb <= 1 ) { size_kb = 1; }
        QString strSize = QString::number( size_kb ) + "KB";

        table->setItem( row, 1, new QTableWidgetItem( strSize ) );
        //修改时间
        QString   strDataTime;
        QDateTime time = fileInfo->at( row ).lastModified();
        strDataTime    = time.toString( "yyyy-MM-dd hh:mm:ss" );
        table->setItem( row, 2, new QTableWidgetItem( strDataTime ) );
    }
}

void Readlog::on_pushButton_open_file_clicked() {
    int row = table->currentRow();

    if ( row < 0 ) { return; }
    //  qDebug() << "row:" << row;
    QString path = fileInfo->at( row ).filePath();

    QFile file( path );

    if ( !file.open( QFile::ReadOnly | QFile::Text ) ) {
        QMessageBox::warning( this, tr( "警告" ), tr( "无法读取文件 %1 %2" ).arg( path ).arg( file.errorString() ) );
        return;
    }
    QTextStream in( &file );
    QApplication::setOverrideCursor( Qt::WaitCursor );

    textedit->setText( in.readAll() );  //大型文件不适用readAll()

    QApplication::restoreOverrideCursor();
}

void Readlog::on_pushButton_debug_log_clicked() {
    findfiles( "./debug-log/", "Debug" );
}

void Readlog::on_pushButton_crittcal_log_clicked() {
    findfiles( "./crittcal-log/", "Crittcal" );
}

void Readlog::on_pushButton_info_log_clicked() {
    findfiles( "./info-log/", "Info" );
}
